

package com.nn.smart.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nn.smart.bus.commons.mybatis.service.impl.BaseServiceImpl;
import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.ConvertUtils;
import com.nn.smart.bus.commons.tools.constant.FieldConstant;
import com.nn.smart.bus.dao.NnclNnbusDictionaryDao;
import com.nn.smart.bus.dto.NnclNnbusDictionaryDTO;
import com.nn.smart.bus.entity.NnclNnbusDictionaryEntity;
import com.nn.smart.bus.redis.NnclNnbusDictionaryRedis;
import com.nn.smart.bus.service.NnclNnbusDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 公交车辆类型 
 *
 * @author nanning nanning@123.com
 * @since v1.0.0 2021-08-25
 */
@Service
public class NnclNnbusDictionaryServiceImpl extends BaseServiceImpl<NnclNnbusDictionaryDao, NnclNnbusDictionaryEntity> implements NnclNnbusDictionaryService {

    @Autowired
    private NnclNnbusDictionaryRedis nnclNnbusDictionaryRedis;

    @Override
    public PageData<NnclNnbusDictionaryDTO> page(Map<String, Object> params) {
        IPage<NnclNnbusDictionaryEntity> page = baseDao.selectPage(
                getPage(params, FieldConstant.CREATED_TIME, false),
                getWrapper(params)
        );
        return getPageData(page, NnclNnbusDictionaryDTO.class);
    }

    @Override
    public List<NnclNnbusDictionaryDTO> list(Map<String, Object> params) {
        List<NnclNnbusDictionaryEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, NnclNnbusDictionaryDTO.class);
    }

    private QueryWrapper<NnclNnbusDictionaryEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get(FieldConstant.ID_HUMP);

        QueryWrapper<NnclNnbusDictionaryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), FieldConstant.ID, id);

        return wrapper;
    }

    @Override
    public NnclNnbusDictionaryDTO get(String id) {
        NnclNnbusDictionaryEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, NnclNnbusDictionaryDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NnclNnbusDictionaryDTO dto) {
        NnclNnbusDictionaryEntity entity = ConvertUtils.sourceToTarget(dto, NnclNnbusDictionaryEntity.class);
        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NnclNnbusDictionaryDTO dto) {
        NnclNnbusDictionaryEntity entity = ConvertUtils.sourceToTarget(dto, NnclNnbusDictionaryEntity.class);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        // 逻辑删除（@TableLogic 注解）
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

}
